from dearpygui.core import *
from dearpygui.simple import *
import os
'''
Passw0rd!

-F means one file
 pip install numpy==1.19.3
pyinstaller --debug all qr_main.py
# pyinstaller  --noconsole --add-data "simsun.ttc;." dear_gui.py

'''
class Dear:
    def des(self):
        with window("说明", x_pos=100, y_pos=100, width=450, no_close=True, no_collapse=True):
            add_text("1、需要加载xlsx格式的excel，含有data页。")
            add_text("2、打印第一列为二维码，第二列为标注。")
            add_text("3、需要加载打印模板。")
            add_text("4、需要安装必要的驱动和软件，但不用打开。")
    
    def on_select(self, sender, data):
        if sender == "选择excel":
            open_file_dialog(callback=lambda sender, data: set_value("excel_path", f"{data[0]}\\{data[1]}"),
                             extensions=".*")
        else:
            open_file_dialog(
                callback=lambda sender, data: set_value("template_path", f"{data[0]}\\{data[1]}"),
                extensions=".*")
    
    def select_file(self):
        with window("选择", x_pos=100, y_pos=350, width=450, no_close=True, no_collapse=True):
            add_button("选择excel", callback=self.on_select)
            add_text("excel地址: ")
            add_same_line()
            add_label_text("##excel", source="excel_path", color=[255, 0, 0])
            
            add_button("选择打印模板", callback=self.on_select)
            add_text("打印模板地址: ")
            add_same_line()
            add_label_text("##template", source="template_path", color=[255, 0, 0])
    
    def on_print(self,sender,data):
        excel = get_value("excel_path")
        template = get_value("template_path")
        if not all([excel,template]):
            add_text("请确保选择了excel和打印模板", parent="output")
            return
        try:
            data=read_excel(excel)
        except Exception as e:
            print (e)
            add_text(f"{e}", parent="output")
            return
        if len(data)==0:
            add_text("excel没有数据", parent="output")
            return
        add_text(excel,parent="output")
        add_text(template,parent="output")
        

    def begin_print(self):
    
        with window("打印", x_pos=600, y_pos=100, width=450,height=450, no_close=True, no_collapse=True):
            add_button("开始打印", callback=self.on_print)
            with child("output"):
                pass
    
    def run(self):
        font = os.path.abspath("./simsun.ttc")
        add_additional_font(font, 16, glyph_ranges='chinese_full')
        self.des()
        self.select_file()
        self.begin_print()
        start_dearpygui()


if __name__ == '__main__':
    d = Dear()
    d.run()
